package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.Helpstar;
import org.springblade.modules.app.service.IHelpstarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 产品管理
 */
@RestController
@RequestMapping("blade-app/helpstar")
public class HelpstarController extends BladeController {

    @Autowired
    private IHelpstarService helpstarService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    public R<Helpstar> detail(Helpstar helpstar) {
        Helpstar detail = helpstarService.getOne(Condition.getQueryWrapper(helpstar));
        return R.data(detail);
    }


    /**
     * 自定义分页 服务之星
     */
    @GetMapping("/page")
    public R<IPage<Helpstar>> page(Page page, Helpstar params) {
        LambdaQueryWrapper<Helpstar> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(params.getPname())) {
            queryWrapper.and(wrapper -> wrapper.like(Helpstar::getPname, params.getPname()));
        }
        queryWrapper.orderByDesc(Helpstar::getCtime);
        IPage<Helpstar> pages = helpstarService.page(page, queryWrapper);
        return R.data(pages);
    }

    @GetMapping("/list")
    public List<Helpstar> page(Helpstar params) {
        List<Helpstar> result = helpstarService.list();
        return result;
    }

    /**
     * 新增 服务之星
     */
    @PostMapping("/save")
    public R save(@RequestBody Helpstar helpstar) {
        helpstar.setCtime(new Date());
        return R.status(helpstarService.save(helpstar));
    }

    /**
     * 修改 服务之星
     */
    @PostMapping("/update")
    public R update(@RequestBody Helpstar helpstar) {
        return R.status(helpstarService.updateById(helpstar));
    }

    /**
     * 新增或修改 服务之星
     */
    @PostMapping("/submit")
    public R submit(@RequestBody Helpstar helpstar) {
        return R.status(helpstarService.saveOrUpdate(helpstar));
    }


    /**
     * 删除 服务之星
     */
    @PostMapping("/remove")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(helpstarService.removeByIds(Func.toLongList(ids)));
    }


}
